home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 295_01 / bputhf.c < prev    next >
Text File  |  1989-12-28  |  3KB  |  122 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "bputhf.c    1.2 - 89/10/31" */
  5.  
  6. #include <errno.h>
  7. /*#include <stddef.h>*/
  8. /*#include <string.h>*/
  9. #include "blkio_.h"
  10.  
  11. /*man---------------------------------------------------------------------------
  12. NAME
  13.      bputhf - put a header field into a block file
  14.  
  15. SYNOPSIS
  16.      #include <blkio.h>
  17.  
  18.      int bputhf(bp, offset, buf, bufsize)
  19.      BLKFILE *bp;
  20.      size_t offset;
  21.      const void *buf;
  22.      size_t bufsize;
  23.  
  24. DESCRIPTION
  25.      The bputhf function writes the contents of buf into a field of
  26.      the header of the block file associated with BLKFILE pointer bp.
  27.      The field begins offset characters from the beginning of the
  28.      header and is bufsize characters long.  buf must point to a
  29.      storage area at least bufsize characters long.
  30.  
  31.      bputhf will fail if one or more of the following is true:
  32.  
  33.      [EINVAL]       bp is not a valid BLKFILE pointer.
  34.      [EINVAL]       bufsize is less than 1.
  35.      [EINVAL]       buf is the NULL pointer.
  36.      [BEBOUND]      offset + bufsize extends beyond the
  37.                     boundary of the header.
  38.      [BEEOF]        Attempt to write a field before the
  39.                     complete header has been written.
  40.      [BENOPEN]      bp is not open for writing.
  41.  
  42. SEE ALSO
  43.      bgethf, bputh, bputbf.
  44.  
  45. DIAGNOSTICS
  46.      Upon successful completion, a value of 0 is returned.  Otherwise,
  47.      a value of -1 is returned, and errno set to indicate the error.
  48.  
  49. NOTES
  50.      When the header is created,  the entire header must be written,
  51.      so offset must have a value of 0 and bufsize must be equal to the
  52.      block size.
  53.  
  54. ------------------------------------------------------------------------------*/
  55. int bputhf(bp, offset, buf, bufsize)
  56. BLKFILE *bp;
  57. size_t offset;
  58. CONST void *buf;
  59. size_t bufsize;
  60. {
  61.     /* validate arguments */
  62.     if (!b_valid(bp) || (buf == NULL) || (bufsize < 1)) {
  63.         errno = EINVAL;
  64.         return -1;
  65.     }
  66.  
  67.     /* check if not open for reading */
  68.     if (!(bp->flags & BIOWRITE)) {
  69.         errno = BENOPEN;
  70.         return -1;
  71.     }
  72.  
  73.     /* check if block boundary is crossed */
  74.     if ((offset + bufsize) > bp->hdrsize) {
  75.         errno = BEBOUND;
  76.         return -1;
  77.     }
  78.  
  79.     /* check if incomplete header */
  80.     if ((offset != 0) || (bufsize != bp->hdrsize)) {
  81.         if (bp->endblk < 1) {
  82.             errno = BEEOF;
  83.             return -1;
  84.         }
  85.     }
  86.  
  87.     /* check if not buffered */
  88.     if (bp->bufcnt == 0) {
  89.         if (b_uputf(bp, (bpos_t)0, offset, buf, bufsize) == -1) {
  90.             BEPRINT;
  91.             return -1;
  92.         }
  93.         if (bp->endblk < 1) {
  94.             bp->endblk = 1;
  95.         }
  96.         errno = 0;
  97.         return 0;
  98.     }
  99.  
  100.     /* check if buffer is not loaded */
  101.     if (!(b_block_p(bp, (size_t)0)->flags & BLKREAD)) {
  102.         if ((offset != 0) || (bufsize != bp->hdrsize)) {
  103.             if (b_get(bp, (size_t)0) == -1) {    /* read block from file */
  104.                 if (errno != BEEOF) BEPRINT;
  105.                 return -1;
  106.             }
  107.         }
  108.     }
  109.  
  110.     /* copy from buf into block buffer and set flags */
  111.     memcpy(((char *)b_blkbuf(bp, (size_t)0) + offset), buf, bufsize);
  112.     b_block_p(bp, (size_t)0)->flags = BLKREAD | BLKWRITE;
  113.  
  114.     /* adjust endblk */
  115.     if (bp->endblk < 1) {
  116.         bp->endblk = 1;
  117.     }
  118.  
  119.     errno = 0;
  120.     return 0;
  121. }
  122.